﻿using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMS.Model;
using WMS.Repository.IRepository;

namespace WMS.Repository
{
    public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, IBaseModel, new()
    {
        protected readonly ISqlSugarClient _db;
        protected readonly ILogger<BaseRepository<TEntity>> _logger;

        public BaseRepository(ISqlSugarClient db, ILogger<BaseRepository<TEntity>> logger)
        {
            _db = db;
            _logger = logger;
        }

        /// <summary>
        /// 添加
        /// </summary>
        public async Task<bool> AddAsync(TEntity entity)
        {
            try
            {
                return await _db.Insertable(entity).ExecuteCommandAsync() > 0;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"添加实体失败 - 类型: {typeof(TEntity).Name}");
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        public async Task<int> DeleteAsync<TEntity>(int id) where TEntity : class, IBaseModel, new()
        {
            try
            {
                return await _db.Deleteable<TEntity>().Where(x => x.Id == id).ExecuteCommandAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"删除实体失败 - 类型: {typeof(TEntity).Name}, ID: {id}");
                throw;
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        public async Task<TEntity> GetAsync(long id)
        {
            try
            {
                return await _db.Queryable<TEntity>().Where(x => x.Id == id).FirstAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"查询实体失败 - 类型: {typeof(TEntity).Name}, ID: {id}");
                throw;
            }
        }

        /// <summary>
        /// 查询全部
        /// </summary>
        public async Task<List<TEntity>> QueryAsync()
        {
            try
            {
                return await _db.Queryable<TEntity>().ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"查询实体列表失败 - 类型: {typeof(TEntity).Name}");
                return null;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        public async Task<bool> UpdateAsync(TEntity entity)
        {
            try
            {
                return await _db.Updateable(entity).ExecuteCommandAsync() > 0;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"修改实体失败 - 类型: {typeof(TEntity).Name}, ID: {entity?.Id}");
                throw;
            }
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        public async Task<TEntity> GetById(long id)
        {
            try
            {
                return await _db.Queryable<TEntity>().Where(m => m.Id == id).FirstAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"根据ID查询失败 - 类型: {typeof(TEntity).Name}, ID: {id}");
                throw;
            }
        }
    }
}